package com.ltba.rf.control.beeper;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class BeeperManager {

	private ScheduledExecutorService svc;
	private ScheduledFuture<?> sche;

	private void doStart(int i) {
		if (null == svc || svc.isShutdown()) {
			svc = Executors.newSingleThreadScheduledExecutor();
		}
		sche = svc.scheduleAtFixedRate(new Beeper(i), 0, 3, TimeUnit.SECONDS);
	}

	private void doStop() {
		sche.cancel(true);
		svc.shutdown();
	}
	
	public void beep(int type, int duration) {
		doStart(type);
		try {
			Thread.sleep(1000 * duration);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		doStop();
	}
	
	public static void main(String [] args) throws InterruptedException {
		BeeperManager mg = new BeeperManager();
		mg.beep(2, 10);
	}
}
